﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Web.System.Services.Sites;
using SaaS.Model.Dto.Sites;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using XCore.Domain.Dto;
using SaaS.Core.Authorization;
using SaaS.Cache.Sites;
using XCore.Authorization;

namespace Web.System.Controllers.Sites
{
    /// <summary>
    /// 订单管理
    /// </summary>
    [Route("api/site/[controller]/[action]")]
    [ApiController]
    public class OrderController : SystemControllerBase
    {
        private readonly SiteCacheService _siteCacheService;
        public OrderController(SiteCacheService siteCacheService)
        {
            _siteCacheService = siteCacheService;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpPost, Permission(PermissionNames.Site_Order)]
        public ActionResult List()
        {
            var query = SDC.Orders.AsQueryable();
            decimal totalPirce = query.Max(c => c.TotalPrice);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.IP,
                c.CreationTime,
                c.Discount,
                c.Id,
                c.PayType,
                c.Price,
                c.ProjectName,
                c.ProjectId,
                c.Status,
                c.TotalPrice,
                c.UserId,
                UserName = _siteCacheService.GetName(c.UserId)
            }, totalPirce);
        }
    }
}